<?php
namespace App\Services;

use Config;
use App\Services\Openssl\CustomsClearanceException;
use Log;


class CustomsClearance{
    protected $config;
    protected $pubkey;

    function __construct()
    {

        $config = config('eorderinfo');
        $this->config = $config;

    }

    /** 设置参数
     * @param $key
     * @param $value
     */
    public function setParam($key,$value){
        $this->params[$key] = $value;
    }

    /*public function sendPost($orders){

            $xmlorder = '<?xml version="1.0" encoding="UTF-8"?>
            <root>
            <pub>
            <requestTime>' . date('Y-m-d H:i:s') . '</requestTime>
            <cbeCode>' . $this->config['cbeCode'] . '</cbeCode>
            </pub>
            <orders>';

            foreach ($orders as $key => $item) {
            $xmlorder .= '
            <order>
            <orderNo>' . $item->order_sn . '</orderNo>
            <waybillNo>' . $item->shipping_num . '</waybillNo>
            </order>';
            }

            $xmlorder .= '
            </orders>
            </root>';


       // echo $xmlorder;
        return $this->makeRequest($this->config['orderStatus'], ['xml' => $xmlorder]);
    }*/


    public function sendPost($orders){

            $xmlorder = '<?xml version="1.0" encoding="UTF-8"?>
            <root>
            <pub>
                    <request_time>' . date('Y-m-d H:i:s') . '</request_time>
                    <user_code>' . $this->config['cbeCode'] . '</user_code>
                    <type>3</type>
            </pub>
            <orders>';

            foreach ($orders as $key => $item) {

                $xmlorder .= '
                    <order>
                        <order_no>' . $item->order_sn . '</order_no>
                        <logistic_no>' . $item->shipping_num . '</logistic_no>
                    </order>';
            }
            $xmlorder .= '
            </orders>
            </root>';

// Log::info('订单清关请求报文：'.$xmlorder);
       // echo $xmlorder;
        return $this->makeRequest($this->config['orderStatus'], ['xml' => $xmlorder]);
    }


    /**
     * 构建请求
     * @param $data
     * @return mixed|string
     */
    private function makeRequest($url, $data)
    {

        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
            $response = curl_exec($ch);
            // echo $response;exit();
// Log::info('清关状态响应报文：'.$response);

        }catch (\Exception $e){

            throw new CustomsClearanceException($e->getMessage());
        }

        $repsonXmlObj = simplexml_load_string($response);

        if($repsonXmlObj->pub->stat==0){


            $orders = (json_decode(json_encode($repsonXmlObj->orders),TRUE));
            $orderArray = [];

            foreach($orders as $key=>$value){


                if (isset($value['order_no'])){
                    $values = $value;

                    $config['customs_clearance'] = config('customs_clearance');

                    $values['status'] = $config['customs_clearance']['status'][$values['status']];
                    $values['dist_status'] = $config['customs_clearance']['dist_status'][$values['dist_status']];
                    $orderArray[$value['order_no']] = $values;


                }else{

                    $max_key = count($value);

                    foreach ($value as $k => $v) {
                        if ($k == ($max_key-1)) {

                            if (isset($v['order_no'])){
                                $values = $v;

                                $config['customs_clearance'] = config('customs_clearance');

                                $values['status'] = $config['customs_clearance']['status'][$values['status']];
                                $values['dist_status'] = $config['customs_clearance']['dist_status'][$values['dist_status']];
                                $orderArray[$v['order_no']] = $values;

                            }
                        }
                    }

                }
            }
            return $orderArray;
        }
        else{

            throw new CustomsClearanceException($repsonXmlObj->Detail);
        }



    }


}